let router = require('../router/Router');
let gameService = require('../service/gameService');
let clubMemberService = require('../service/ClubMemberService');

let stringUtil = require('../util/stringUtil');

const { BusiError, SysError, ErrorResult } = require('../exception/exception');

var unionRoomMap = new Map();

router.post("/auth/game/room/reCreate", (req, resp) => {

    if(!req.body.roomId || req.body.roomId === '') {
        resp.send({code: 500, msg: "data is null"});
        return;
    }

    gameService.reCreateFkGameRoom(req.uid, req.body.roomId).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("game reCreate err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/game/room/create", (req, resp) => {
    if(!req.body.gt || req.body.gt === '') {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if(!req.body.data || req.body.data === '') {
        resp.send({code: 500, msg: "data is null"});
        return;
    }

    gameService.createFkGameRoom(req.uid, req.body.gt, req.body.clubID, req.body.data).then(res => {
        resp.send({code: 200, msg: "success", data: res});
     }).catch(err => {
        console.logw("game create err:", err.message);
         resp.send(new ErrorResult(err));
     });
});

router.post("/auth/game/room/createunion", (req, resp) => {
    if(!req.body.gt || req.body.gt === '') {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if(!req.body.data || req.body.data === '') {
        resp.send({code: 500, msg: "data is null"});
        return;
    }

    gameService.createUnionGameRoom(req.uid, req.body.gt, req.body.unionID, req.body.data).then(res => {
        //add
        let mymap = {};
        mymap.uid = req.uid;
        mymap.gt = req.body.gt;
        mymap.unionID = req.body.unionID;
        mymap.data = req.body.data;
        mymap.roomid = res.roomID;
        mymap.ts = res.ts;

        let key = mymap.gt + mymap.roomid + mymap.ts;

        unionRoomMap[key] = mymap;

        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("game create err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/game/errorInfo", (req, resp) => {

    gameService.insertErrorInfo(req.uid, req.body.roomId, req.body.clubId, req.body.gameType, req.body.info, req.body.ts).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("game create err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/club/member/buffer/update", (req, resp) => {
    if(stringUtil.isBlank(req.body.uid)){
        resp.send({code: 500, msg: "uid不能为空"});
        return;
    }

    if(stringUtil.isBlank(req.body.clubID)){
        resp.send({code: 500, msg: "clubID不能为空"});
        return;
    }

    if(req.body.buffer >= 0){
        resp.send({code: 500, msg: "buffer不能大于等于0"});
        return;
    }

    clubMemberService.updateBuffer(req.body.clubID, req.body.uid, req.body.buffer).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("club buffer update err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/player/buffer/update", (req, resp) => {
    if(stringUtil.isBlank(req.body.uid)){
        resp.send({code: 500, msg: "uid不能为空"});
        return;
    }

    if(req.body.buffer >= 0){
        resp.send({code: 500, msg: "buffer不能大于等于0"});
        return;
    }

    gameService.upateUsersBuffer(req.body.uid, req.body.buffer).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("club buffer update err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/game/club/room/query", (req, resp) => {
    gameService.queryClubRoom(req.body.clubID).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query club room err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/game/union/room/query", (req, resp) => {
    gameService.queryUnionRoom(req.body.unionID).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query club room err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/game/club/room/join", (req, resp) => {
    gameService.joinClubRoom(req.body.clubID).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query club room err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/game/union/room/join", (req, resp) => {
    gameService.joinUnionRoom(req.body.unionID).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query club room err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

//俱乐部基金清单
router.post("/auth/game/club/inventory", (req, resp) => {
    console.logw("玩家发来数据 ", req.body.clubid,req.body.page);
    gameService.getClubBill(req.uid,req.body.clubid,req.body.page).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query settlement item err:", err.message);
        resp.send(new ErrorResult(err));
    });
});


router.post("/game/room/play/flag", (req, resp) => {
    if(!req.body.gt || req.body.gt === '') {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if(!req.body.roomID || req.body.roomID === '') {
        resp.send({code: 500, msg: "room id is null"});
        return;
    }

    if(!req.body.ts || req.body.ts === '') {
        resp.send({code: 500, msg: "room ts is null"});
        return;
    }
    gameService.setFkRoomGameFlag(req.body.roomID, req.body.gt, req.body.ts, req.body.gc).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("game ticker err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/game/room/user/get", (req, resp) => {
    gameService.getUserRoomInfo(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("game ticker err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/game/room/query", (req, resp) => {
    if(!req.body.roomID || req.body.roomID === '') {
        resp.send({code: 500, msg: "room id is null"});
        return;
    }

    gameService.getFkGameRoomInfo(req.body.roomID, req.body.ts, req.body.gt, req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("room query err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/room/destroy", (req, resp) => {
    if(!req.body.gt || req.body.gt === '') {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if(!req.body.roomID || req.body.roomID === '') {
        resp.send({code: 500, msg: "room id is null"});
        return;
    }

    if(!req.body.ts || req.body.ts === '') {
        resp.send({code: 500, msg: "room ts is null"});
        return;
    }

    var  gameCount=req.body.gameCount;
    if(gameCount==undefined)  gameCount=-1;

    gameService.destroyFkRoom(req.body.roomID, req.body.gt, req.body.ts,gameCount).then(res => {
        //这里如果是大联盟的话创建一个房间
        //mymap.gt + mymap.roomid + mymap.ts;
        let key = req.body.gt + req.body.roomID + req.body.ts;
        for (var inkey in unionRoomMap) {
            if(inkey === key){
                let uid = unionRoomMap[key].uid;
                let gt = unionRoomMap[key].gt;
                let unionID = unionRoomMap[key].unionID;
                let data = unionRoomMap[key].data;
                //gameService.createUnionGameRoom(uid,gt,unionID,data);
                delete unionRoomMap[key];
                break;
            }
        }

        //unionRoomMap
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("game ticker err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/room/enter", (req, resp) => {
    if(!req.body.uid || req.body.uid === '') {
        resp.send({code: 500, msg: "uid is null"});
        return;
    }

    if(!req.body.gt || req.body.gt === '') {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if(!req.body.roomID || req.body.roomID === '') {
        resp.send({code: 500, msg: "room id is null"});
        return;
    }

    gameService.enterRoom(req.body.uid, req.body.roomID, req.body.gt, req.body.clubID).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("enter room err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/room/leave", (req, resp) => {
    if(!req.body.uid || req.body.uid === '') {
        resp.send({code: 500, msg: "uid is null"});
        return;
    }

    if(!req.body.gt || req.body.gt === '') {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if(!req.body.roomID || req.body.roomID === '') {
        resp.send({code: 500, msg: "room id is null"});
        return;
    }

    gameService.leaveRoom(req.body.uid, req.body.roomID, req.body.gt).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("leave room err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/room/ticker", (req, resp) => {
    if(!req.body.gt || req.body.gt === '') {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if(!req.body.roomID || req.body.roomID === '') {
        resp.send({code: 500, msg: "room id is null"});
        return;
    }

    gameService.roomTicker(req.body.roomID, req.body.gt).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("game ticker err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/room/updatePlayNum", (req, resp) => {
    if(!req.body.gt || req.body.gt === '') {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if(req.body.playNum === undefined || req.body.playNum === '') {
        resp.send({code: 500, msg: "playNum id is null"});
        return;
    }

    gameService.serverUpdatePlayNum(req.body.playNum, req.body.gt).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("game playNum err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/room/check", (req, resp) => {
    if(!req.body.gt || req.body.gt === '') {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if(!req.body.roomID || req.body.roomID === '') {
        resp.send({code: 500, msg: "room id is null"});
        return;
    }
    
    gameService.checkFkRoomInfo(req.body.roomID, req.body.gt).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("game ticker err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

/**
 * @param
 *  gameType
 *  roomType
 */
router.post("/auth/game/desktop/list", (req, resp) => {
    if (!req.body.gameType) {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if (!req.body.roomType) {
        resp.send({code: 500, msg: "room type is null"});
        return;
    }

    gameService.queryDeskInfos(req.body.gameType, req.body.roomType, 10000, req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query desk infos err:", err);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/game/desktop/pwd/set", (req, resp) => {
    if (!req.body.gameType) {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if (!req.body.roomType) {
        resp.send({code: 500, msg: "room type is null"});
        return;
    }

    if (!req.body.roomId) {
        resp.send({code: 500, msg: "room id is null"});
        return;
    }

    gameService.setDeskPwd({
        gameType: req.body.gameType,
        roomType: req.body.roomType,
        roomId: req.body.roomId,
        owner: req.uid
    }).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("query desk infos err:", err);
        resp.send(new ErrorResult(err));
    });
});

/**
 * @param
 *  gameType {int}
 *  roomType {int}
 *  roomId {string}
 *  roomInfo:
 *      roomStatus {int}
 *      playerInfos {array}
 */
router.post("/game/desktop/set", (req, resp) => {
    // cacheKey
    if (!req.body.gameType) {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if (!req.body.roomType) {
        resp.send({code: 500, msg: "room type is null"});
        return;
    }

    if (!req.body.roomId) {
        resp.send({code: 500, msg: "room id is null"});
        return;
    }

    gameService.setDeskInfos({
        gameType:req.body.gameType,
        roomType:req.body.roomType,
        roomId: req.body.roomId,
        info: req.body
    }).then(() => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("set desk infos err:", err);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/user/result/set", (req, resp) => {
    // cacheKey
    if (!req.body.gameType) {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if (!req.body.uid) {
        resp.send({code: 500, msg: "uid is null"});
        return;
    }

    if (!req.body.info) {
        resp.send({code: 500, msg: "info is null"});
        return;
    }

    gameService.appendUserGameResult({uid: req.body.uid, gameType: req.body.gameType, info: req.body.info}).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("err:", err);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/game/user/result/query", (req, resp) => {
    //queryUserGameResult
    if (!req.body.gameType) {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    gameService.queryUserGameResult({uid: req.uid, gameType: req.body.gameType}).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("err:", err);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/desktop/pwd/check", (req, resp) => {
    if (!req.body.gameType) {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if (!req.body.roomType) {
        resp.send({code: 500, msg: "room type is null"});
        return;
    }

    if (!req.body.roomId) {
        resp.send({code: 500, msg: "room id is null"});
        return;
    }

    gameService.isDeskNeedPwd({
        gameType: req.body.gameType,
        roomType: req.body.roomType,
        roomId: req.body.roomId
    }).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("check desk pwd err:", err);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/desktop/pwd/valid", (req, resp) => {
    if (!req.body.gameType) {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if (!req.body.roomType) {
        resp.send({code: 500, msg: "room type is null"});
        return;
    }

    if (!req.body.roomId) {
        resp.send({code: 500, msg: "room id is null"});
        return;
    }

    gameService.validDeskPwd({
        gameType: req.body.gameType,
        roomType: req.body.roomType,
        roomId: req.body.roomId,
        pwd: req.body.pwd
    }).then(() => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("valid desk pwd err:", err);
        resp.send(new ErrorResult(err));
    });
});

/**
 * @param
 *  gameType {int}
 *  roomType {int}
 *  roomId {string}
 */
router.post("/game/desktop/get", (req, resp) => {
    if (!req.body.gameType) {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if (!req.body.roomType) {
        resp.send({code: 500, msg: "room type is null"});
        return;
    }

    if (!req.body.roomId) {
        resp.send({code: 500, msg: "room id is null"});
        return;
    }
    resp.send({code: 500, msg: "room id is null"});
});

/**
 * @param
 *  gameType {int}
 */
router.post("/game/desktop/clear", (req, resp) => {
    if (!req.body.gameType) {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    gameService.clearDeskInfos(req.body.gameType, req.body.roomType).then(()=>{
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("clear desk infos err:", err);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/game/record", (req, resp) => {
    let page = 1;
    if (req.body.page && req.body.page >= 1){
        page = req.body.page;
    }
    gameService.getGameRecord(req.uid, page).then((res) => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("ask question err:", err);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/startup", (req, resp) => {
    if(!req.body.gt){
        resp.send({code: 500, msg: "gt must be not null"});
        return;
    }

    if(!req.body.host){
        resp.send({code: 500, msg: "host must be not null"});
        return;
    }

    if(!req.body.port){
        resp.send({code: 500, msg: "port must be not null"});
        return;
    }

    if(req.body.path === null || req.body.path === undefined){
        resp.send({code: 500, msg: "path must be not null"});
        return;
    }

    if(!req.body.scheme){
        resp.send({code: 500, msg: "scheme must be not null"});
        return;
    }

    gameService.gameStartup(req.body.gt, req.body.host, req.body.port, req.body.path, req.body.scheme).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("game startup err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/ticker", (req, resp) => {
    if(!req.body.gt){
        resp.send({code: 500, msg: "gt must be not null"});
        return;
    }

    gameService.gameTicker(req.body.gt).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("game ticker err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

//快三 游戏结束存储消息
router.post("/game/GameOverToSave", (req, resp) => {
    gameService.gameOverToSave(req.body).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("game GameOverToSave err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

//快三 玩家存储输赢消息
router.post("/game/PlayerBetInfo", (req, resp) => {
    gameService.playerBetInfo(req.body).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("game PlayerBetInfo err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/flag/set", (req, resp) => {
    resp.send({code: 200, msg: "success"});
    return;
    if(!req.body.uid){
        resp.send({code: 500, msg: "uid must not be null"});
        return;
    }

    if(!req.body.gt){
        resp.send({code: 500, msg: "gt must not be null"});
        return;
    }

    if(!req.body.rt){
        resp.send({code: 500, msg: "rt must not be null"});
        return;
    }

    gameService.setGameFlagGaming(req.body.uid, req.body.gt, req.body.rt).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("game flag set err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/game/flag/clear", (req, resp) => {
    resp.send({code: 200, msg: "success"});
    return;

    
    if(!req.body.uid){
        resp.send({code: 500, msg: "uid must not be null"});
        return;
    }

    if(!req.body.gt){
        resp.send({code: 500, msg: "gt must not be null"});
        return;
    }

    gameService.clearGameGamingFlag(req.body.uid, req.body.gt).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("game flag clear err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/gateway/auth/server/game/get", (req, resp) => {
    if(!req.body.gt){
        resp.send({code: 500, msg: "gt must not be null"});
        return;
    }

    gameService.getGameServerInfo(req.body.gt).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("game flag err:", err.message);
        resp.send(new ErrorResult(err));
    });
});

router.post("/auth/game/flag", (req, resp) => {
    if(!req.body.gt){
        resp.send({code: 500, msg: "gt must not be null"});
        return;
    }

    gameService.getGameFlagGaming(req.uid, req.body.gt).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("game flag err:", err.message);
        resp.send(new ErrorResult(err, err.data));
    });
});

router.post("/game/info/user/online", (req, resp) => {
    // cacheKey
    if (!req.body.gt) {
        resp.send({code: 500, msg: "game type is null"});
        return;
    }

    if (req.body.cnt === null && req.body.cnt === undefined) {
        resp.send({code: 500, msg: "cnt is null"});
        return;
    }

    // console.log("online user:", req.body.gt, req.body.cnt);
    gameService.updateOnlineUserCnt(req.body.gt, req.body.cnt).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("game flag err:", err.message);
        resp.send(new ErrorResult(err));
    });
});



router.post("/game/info/user/updateRoomUsers", (req, resp) => {
    gameService.updateRoomUsers(req.body.roomId, req.body.usersInfo).then(res => {
        resp.send({code: 200, msg: "success"});
    }).catch(err => {
        console.logw("game flag err:", err.message);
        resp.send(new ErrorResult(err));
    });

})


router.post("/auth/info/user/getRoomUsers", (req, resp) => {
    gameService.getRoomUsers(req.body.roomId,req.uid).then(res => {
        resp.send({code: 200, msg: "success",usersInfo:res});
    }).catch(err => {
        console.logw("game flag err:", err.message);
        resp.send(new ErrorResult(err));
    });
})

router.post("/auth/info/user/getRoomUsers", (req, resp) => {
    gameService.getRoomUsers(req.body.roomId,req.uid).then(res => {
        resp.send({code: 200, msg: "success",usersInfo:res});
    }).catch(err => {
        console.logw("game flag err:", err.message);
        resp.send(new ErrorResult(err));
    });
})

router.post("/sys/info/getGameUserCount", (req, resp) => {
    gameService.getUserCount().then(res => {
        resp.send({code: 200, msg: "success",usersInfo:res});
    }).catch(err => {
        console.logw("game flag err:", err.message);
        resp.send(new ErrorResult(err));
    });
})
